<template>
   <div>
    <MyHeader title="购物车案例"></MyHeader>
    <div class="main">
      <MyGoods 
      :list=list
      @change="changeFn"
      @add="addFn"
      @less="lessFn"
      ></MyGoods>
    </div>
    <MyFooter :list=list></MyFooter>
  </div>
</template>

<script>
import axios from 'axios'
import MyHeader from './components/MyHeader'
import MyGoods from './components/MyGoods'
import MyFooter from './components/MyFooter'
export default {
  components: {
    MyHeader,
    MyGoods,
    MyFooter
  }, 
  methods:{
    changeFn(id){
      const obj = this.list.find(item => item.id === id)
      obj.goods_state = !obj.goods_state
      console.log(obj);
    },
    addFn(id){
     const obj=this.list.find((item)=> item.id===id)
      obj.goods_count=obj.goods_count + 1
    },
    //减
    lessFn(id){
      const obj=this.list.find((item)=>item.id===id)
      if(obj.goods_count>1) return obj.goods_count=obj.goods_count - 1
    },
  },
  data() {
    return {
      list: []
    }
  },
  async created(){
  const {data}= await axios.get('https://www.escook.cn/api/cart')
  this.list=data.list
  },
}
</script>

<style>
  .main{
    padding-top: 45px;
    padding-bottom: 50px;
  }
</style>